package com.tanhua.manage.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.enums.PublishAuditStateEnum;
import com.tanhua.manage.mapper.LogMapper;
import com.tanhua.manage.pojo.Log;
import com.tanhua.manage.pojo.PublishInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LogService extends ServiceImpl<LogMapper, Log> {

    public IPage<Log> findInfoByPage(Integer page, Integer pageSize, String sortProp, String sortOrder, String id, Long sd, Long ed, Long uid, String state) {

        Page<Log> infoPage = new Page<>(page, pageSize);
        if (StrUtil.isNotEmpty(sortProp) && StrUtil.isNotEmpty(sortOrder)) {
            //设置排序
            sortProp = StrUtil.toUnderlineCase(sortProp);
            if ("ascending".equals(sortOrder)) {
                infoPage.setAsc(sortProp);
            } else {
                infoPage.setDesc(sortProp);
            }
        }
        LambdaQueryWrapper<Log> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(id)) {
            lambdaQueryWrapper.like(Log::getUserId, id);
        }
        if (sd != null && ed != null && sd != -1 && sd.equals(ed)) {
            // 当 开始时间 和 结束时间 为相同时，即同一天。结束时间+1天毫秒数
            ed = ed + 86400000;
        }


        if (sd != null && sd > 0) {
            lambdaQueryWrapper.ge(Log::getCreated, sd);
        }
        if (ed != null && ed > 0) {
            lambdaQueryWrapper.le(Log::getCreated, ed);
        }
        if (uid != null) {
            lambdaQueryWrapper.eq(Log::getUserId, uid);
        }
        return super.getBaseMapper().selectPage(infoPage, lambdaQueryWrapper);
    }
}